package com.pj.point.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.widget.SearchView;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.pj.point.R;
import com.pj.point.adapter.HomeListAdapter;
import com.pj.point.home.CreateDiary;
import com.pj.point.litepal.CreateData;
import com.pj.point.litepal.DiaryJavaBean;
import com.pj.point.util.alert.AlertThen;
import com.pj.point.util.alert.AlertUtil;

import org.litepal.LitePal;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;


public class HomeFragment extends Fragment {

    Activity activity;
    private SearchView search;
    private ListView homeList;
    private CardView create;
    private List<DiaryJavaBean> all;
    private ImageView f_button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);
        getData();
        return view;
    }

    private void getData() {
        all = LitePal.findAll(DiaryJavaBean.class, true);
        if (all != null) {
            all = all.stream().filter(diaryJavaBean -> !diaryJavaBean.getIsPrivate()).collect(Collectors.toList());
            homeList.setAdapter(new HomeListAdapter(all));
            homeList.setOnItemClickListener((adapterView, view, i, l) -> {
                Intent intent = new Intent(activity, CreateDiary.class);
                intent.putExtra("isOpen", all.get(i).getCreateTime());
                startActivityForResult(intent,1);
            });
            homeList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                    AlertUtil.show(activity, "删除提醒", "确定要删除篇日记吗？", new AlertThen() {
                        @Override
                        public void then(boolean b) {
                            if (b) {
                                all.get(i).delete();
                                getData();
                            }
                        }
                    });
                    return true;
                }
            });
        }
    }

    private void initView(View view) {
        search = (SearchView) view.findViewById(R.id.search);
        search.setAnimation(AnimationUtils.loadAnimation(activity,R.anim.ltem));
        homeList = (ListView) view.findViewById(R.id.home_list);
        create = (CardView) view.findViewById(R.id.create);
        f_button = (ImageView) view.findViewById(R.id.f_button);
        create.setOnClickListener(v -> {
            startActivityForResult(new Intent(activity, CreateDiary.class), 1);
        });
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (all != null && all.size() != 0) {
                    if (!query.equals("")) {
                        List<DiaryJavaBean> collect = all.stream().filter(diaryJavaBean -> diaryJavaBean.getTitle().contains(query)).collect(Collectors.toList());
                        if (collect.size() > 0) {
                            homeList.setAdapter(new HomeListAdapter(collect));
                            f_button.setImageResource(R.drawable.restart);
                            create.setOnClickListener(vv -> {
                                homeList.setAdapter(new HomeListAdapter(all));
                                f_button.setImageResource(R.drawable.pen);
                                create.setOnClickListener(vvv -> {
                                    startActivityForResult(new Intent(activity, CreateDiary.class), 1);
                                });
                            });
                        } else {
                            Toast.makeText(activity, "未找到相关日记", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    @Override
    public void onResume() {
        getData();
        super.onResume();
    }
}